package com.autonavi.yunda.yunji.controller;

import com.autonavi.yunda.yunji.common.enums.EditType;
import com.autonavi.yunda.yunji.controller.request.TaskConfigRequest;
import com.autonavi.yunda.yunji.core.enums.OperationRecordType;
import com.autonavi.yunda.yunji.core.model.condition.TaskConfigQueryCondition;
import com.autonavi.yunda.yunji.core.model.dto.TaskConfigDTO;
import com.autonavi.yunda.yunji.core.service.TaskConfigService;
import com.autonavi.yunda.yunji.core.vo.response.TaskConfigPageVO;
import com.autonavi.yunda.yunji.service.BucUserService;
import com.autonavi.yunda.yunji.service.OperationRecordService;
import com.autonavi.yunda.yunji.service.ProjectAuthService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cizhong.wcs
 * @date 2022/3/7 7:33 下午
 */
@RestController
@RequestMapping("/taskConfig")
public class TaskConfigController {

    @Resource
    private TaskConfigService taskConfigService;
    @Resource
    private OperationRecordService operationRecordService;
    @Resource
    private BucUserService bucUserService;
    @Resource
    private ProjectAuthService projectAuthService;

    @PostMapping("list")
    public TaskConfigPageVO list(@Validated @RequestBody TaskConfigQueryCondition request) {
        List<String> projectIds = projectAuthService.listAuthorizedProjectIds();
        request.setProjectIds(projectIds);
        return taskConfigService.listByCondition(request);
    }

    @GetMapping("getDetailById")
    public TaskConfigDTO getDetailById(Long id) {
        return taskConfigService.findByIdOrThrow(id);
    }

    @PostMapping("add")
    public Long add(@Validated @RequestBody TaskConfigRequest request) {
        Long id = taskConfigService.add(request.toAddDTO(bucUserService.getCurEmployeeId()));
        operationRecordService.add(OperationRecordType.TaskSubType.CREATE, id.toString());
        return id;
    }


    @PostMapping("update")
    public String update(@Validated @RequestBody TaskConfigRequest request) {
        UpdateWrapper<TaskConfigDTO> updateWrapper = request.toUpdateWrapper(bucUserService.getCurEmployeeId());
        taskConfigService.findByIdOrThrow(request.getId());
        taskConfigService.update(updateWrapper);
        operationRecordService.add(OperationRecordType.TaskSubType.UPDATE, request.getId().toString());
        return "success";
    }

    @GetMapping("publish")
    public void publish(@RequestParam Long id) {
        projectAuthService.checkProjectPermission(id, EditType.TASK.name());
        taskConfigService.publish(id);
        taskConfigService.notifyExecutors();
        operationRecordService.add(OperationRecordType.TaskSubType.PUBLISH, id.toString());
    }

    /**
     * 下线
     */
    @GetMapping("offline")
    public void offline(@RequestParam Long id) {
        projectAuthService.checkProjectPermission(id, EditType.TASK.name());
        taskConfigService.offline(id);
        taskConfigService.notifyExecutors();
        operationRecordService.add(OperationRecordType.TaskSubType.OFFLINE, id.toString());
    }

    /**
     * 进入待验证(预发,生产使用)
     */
    @PostMapping("enterValidating")
    public void enterValidating(@RequestBody TaskConfigRequest request) {
        projectAuthService.checkProjectPermission(request.getProjectId());
        String empId = bucUserService.getCurEmployeeIdOrNull();
        Long id = taskConfigService.enterValidating(request.toValidatingDTO(empId));
        operationRecordService.add(OperationRecordType.TaskSubType.ENTER_VALIDATING, id.toString());
    }


}
